package org.zjut.db;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.zjut.util.CommonUtil;

import javax.xml.transform.Result;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author Rising
 * @date 2017/9/13
 * @des 课程管理
 */
public class CourseDataBase extends JdbcDaoSupport {
    String dataBase = "pso_recommond.";
    String tb_course = "course";

    /**
     * 新增课程
     * 1.课程名称
     * 2.课程描述
     *
     * @return
     */
    public Map<String, Object> addCourse(Map<String, Object> params) {
        String courseName = null, describe = null, userCode = null;
        Map<String, Object> result = new HashMap<>();
        try {
            userCode = params.get("userCode").toString();
            courseName = params.get("courseName").toString();
            describe = params.get("describes").toString();
        } catch (Exception e) {
            return CommonUtil.setErrorInfo("请检查参数");
        }
        String sql = "INSERT INTO " + dataBase + tb_course + "(code,name,_describe,userCode)VALUES(?,?,?,?) ";
        System.out.println(sql);
        try {
            String code = UUID.randomUUID().toString();
            int row = getJdbcTemplate().update(sql, new Object[]{code, courseName, describe, userCode});
            if (row > 0) {
                Map<String, Object> message = new HashMap<>();
                message.put("courseCode", code);
                message.put("courseName", courseName);
                message.put("userCode", userCode);
                message.put("describes", describe);
                result.put("success", true);
                result.put("message", message);
            } else {
                return CommonUtil.setErrorInfo("插入失败,0条影响");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage()+" ");
            return CommonUtil.setErrorInfo("插入失败");
        }
        return result;
    }

    /**
     * 修改课程信息
     * 修改课程的时候，就不需要传userCode了
     *
     * @param params
     * @return
     */
    public Map<String, Object> editCourse(Map<String, Object> params) {
        String courseCode = null, courseName = null, describe = null;
        Map<String, Object> result = new HashMap<>();
        try {
            courseCode = params.get("courseCode").toString();
            courseName = params.get("courseName").toString();
            describe = params.get("describes").toString();
        } catch (Exception e) {
            return CommonUtil.setErrorInfo("请检查参数");
        }
        String sql = "UPDATE " + dataBase + tb_course + " SET name = ? , _describe = ?  WHERE code = ? ";
        try {
            int row = getJdbcTemplate().update(sql, new Object[]{courseName, describe, courseCode});
            if (row > 0) {
                Map<String, Object> message = new HashMap<>();
                message.put("describes", describe);
                result.put("success", true);
                result.put("message", "修改成功");
            } else {
                return CommonUtil.setErrorInfo("修改失败");
            }
        } catch (Exception e) {
            return CommonUtil.setErrorInfo("修改失败");
        }

        return result;
    }

    /**
     * 获取课程
     * 1.根据userCode来获取相应的课程
     * 2.如果没有传userCOde，默认返回所有的课程信息
     *
     * @param params
     * @return
     */
    public Map<String, Object> getCourse(Map<String, Object> params) {
        String userCode = null, courseCode = null;
        Map<String, Object> result = new HashMap<>();
        try {
            if (params.containsKey("userCode")) {
                userCode = params.get("userCode").toString();
            }
            if (params.containsKey("courseCode")) {
                courseCode = params.get("courseCode").toString();
            }
        } catch (Exception e) {
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "SELECT a.code as courseCode,a.name as courseName, a._describe as describes , a.userCode as userCode FROM " + dataBase + tb_course + " AS a " +
                " WHERE a.isDelete = 0 ";
        if (userCode != null) {
            sql = sql + " AND a.userCode = '" + userCode + "'";
        }
        if (courseCode != null) {
            sql = sql + " AND a.courseCode = '" + courseCode + "'";
        }
        try {
            List<Map<String, Object>> listCourse = getJdbcTemplate().queryForList(sql);
            Map<String, Object> message = new HashMap<>();
            message.put("course",listCourse);
            result.put("success", true);
            result.put("message", message);
        } catch (Exception e) {
            return CommonUtil.setErrorInfo("查询失败");
        }

        return result;
    }

    /**
     * 由于课程-知识点-资源等有着相互关联，所有删除都是假删除
     *
     * @return
     */
    public Map<String, Object> delCourse(Map<String, Object> params) {
        String courseCode = null;
        Map<String, Object> result = new HashMap<>();
        try {
            courseCode = params.get("courseCode").toString();
        } catch (Exception e) {
            return CommonUtil.setErrorInfo("请检查参数");
        }
        String sql = " UPDATE " + dataBase + tb_course + " SET isDelete = 1 WHERE code = '" + courseCode + "'";

        try {
            int row = getJdbcTemplate().update(sql);
            if (row > 0) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                return CommonUtil.setErrorInfo("删除失败");
            }
        } catch (Exception e) {
            return CommonUtil.setErrorInfo("删除失败");
        }

        return result;

    }

    /**
     * 获取用户正在学习的课程信息
     * @param userCode
     * @return
     */
    public Map<String,Object> getLearningCourse(String userCode){
        String sql = "SELECT * FROM "+dataBase+"learner_course as a where a.learnerCode = ? and status = 1";
        try {
            Map<String,Object> result = getJdbcTemplate().queryForList(sql,new Object[]{userCode}).get(0);
            return result;
        }catch (Exception e){
            System.out.println("getLearningCourse:"+e.getMessage());
            return null;
        }
    }
}
